package com.Iron.Web;

//import com.Iron.Component.WebApplicationContextUtils;
import com.Iron.Config.SpringConfig1;
import com.Iron.Service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = "/accountServlet")
public class AccountServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //优化：不重复创建销毁工厂
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

/*
        ServletContext con = req.getServletContext();
        ApplicationContext context = (ApplicationContext) con.getAttribute("applicationContext");
        AccountService bean = context.getBean(AccountService.class);
*/
        ServletContext con = req.getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(con);
        AccountService bean = context.getBean(AccountService.class);
        bean.transferMone("小明", "小红", 200);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
